//
//  XUIMenuLabel.m
//  etionUI
//
//  Created by wjxfhxy on 15-1-11.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "XUIMenuLabel.h"

NSString *const UIMenuLabelItemFunPrefix = @"menuItem";

@interface XUIMenuLabel ()
{
    id <XUIMenuLabelDelegate> _menuDelegate;
    
    UIColor *_highLightColor;
    
    BOOL _hiddenMenu;
}

@end

@implementation XUIMenuLabel

@synthesize highLightColor = _highLightColor;

- (id)initWithFrame:(CGRect)frame menuDelegate:(id <XUIMenuLabelDelegate>)menuDelegate
{
    self = [super initWithFrame:frame];

    self.userInteractionEnabled = YES;

    _menuDelegate = menuDelegate;

    UILongPressGestureRecognizer *longGr = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizer:)] autorelease_mm];
    [self addGestureRecognizer:longGr];

    UITapGestureRecognizer *tapGr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPressGestureRecognizer:)] autorelease_mm];
    [self addGestureRecognizer:tapGr];
    
    [NSNotificationCenter addObserver:self dictionarySelector:@{
                    UIMenuControllerWillShowMenuNotification:@"observerUIMenuControllerWillShowMenuNotification:",
                    UIMenuControllerWillHideMenuNotification:@"observerUIMenuControllerWillHideMenuNotification:"
                                                               }];
    _hiddenMenu = YES;
    
    _highLightColor = [[UIColor lightGrayColor] retain_mm];
    
    return self;
}

- (void)dealloc
{
    [_highLightColor release_mm];
    [NSNotificationCenter removeObserver:self];
    superdealloc_mm;
}

- (void)tapPressGestureRecognizer:(UITapGestureRecognizer *)gr
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController.menuVisible == YES)
        [menuController setMenuVisible:NO animated:YES];
}

- (void)longPressGestureRecognizer:(UILongPressGestureRecognizer *)gr
{
    switch (gr.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            UIMenuController *menuController = [UIMenuController sharedMenuController];
            if (menuController.menuVisible == YES)
                return;

            id <XUIMenuLabelDelegate> menuDelegate = _menuDelegate;

            NSMutableArray *ar = [NSMutableArray new];
            for (NSInteger i = 0; i < [menuDelegate numberOfMenuItems]; i++)
            {
                void(^menuItem)(NSString *title, NSString *itemSelector) = ^(NSString *title, NSString *itemSelector)
                {
                    [ar addObject:[[UIMenuItem alloc] initWithTitle:title action:NSSelectorFromString([NSString stringWithFormat:@"%@%@", UIMenuLabelItemFunPrefix, itemSelector])]];
                };

                [menuDelegate menuLabel:self menuItem:menuItem];

            }

            _hiddenMenu = NO;
            
            CGRect frame = self.bounds;
            frame.origin.y += 5;
            frame.size.height -= 5;
            
            [self becomeFirstResponder];
            menuController.menuItems = ar;
            [menuController setTargetRect:frame inView:self];
            [menuController setMenuVisible:YES animated:YES];
            
            break;
        }
        default:
        {
            break;
        }
    }
}

-(void)observerUIMenuControllerWillShowMenuNotification:(NSNotification*)noti
{
    if(_hiddenMenu == YES)
        return;
    
    self.backgroundColor = _highLightColor;
}

-(void)observerUIMenuControllerWillHideMenuNotification:(NSNotification*)noti
{
    _hiddenMenu = YES;
    
    self.backgroundColor = [UIColor clearColor];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    UIMenuController *menuController = sender;
    if ([menuController isKindOfClass:[UIMenuController class]])
    {
        for (UIMenuItem *menuItem in menuController.menuItems)
        {
            if (action == menuItem.action)
            {
                return YES;
            }
        }
    }

    return NO;
}

void menuLabelDyanmicAddMethod(XUIMenuLabel *menuLabel, SEL sel)
{
    NSString *selector = NSStringFromSelector(sel);
    selector = [selector substringFromIndex:UIMenuLabelItemFunPrefix.length];
    SEL s = NSSelectorFromString(selector);
    if ([menuLabel->_menuDelegate respondsToSelector:s])
    {
        [menuLabel->_menuDelegate performSelector:s withObject:menuLabel];
    }
}

+ (BOOL)resolveInstanceMethod:(SEL)sel
{
    NSString *selector = NSStringFromSelector(sel);
    if ([selector hasPrefix:UIMenuLabelItemFunPrefix] == YES)
    {
        class_addMethod(self, sel, (IMP) menuLabelDyanmicAddMethod, NULL);
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

+ (void)hiddenMenuController
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController.menuVisible == YES)
        [menuController setMenuVisible:NO animated:YES];
}

@end
